package cn.edu.hzw.criminalintent.fragmnet

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import cn.edu.hzw.criminalintent.R
import cn.edu.hzw.criminalintent.databinding.FragmentPhotoPreviewDialogBinding
import cn.edu.hzw.criminalintent.utils.PictureUtils

private const val IMG_PATH = "img_path"

class PhotoPreviewDialogFragment : DialogFragment() {
    private lateinit var binding:FragmentPhotoPreviewDialogBinding

   fun newInstance(args:Bundle?):PhotoPreviewDialogFragment{
       val fragment = PhotoPreviewDialogFragment()
       if (args !=null){
           fragment.arguments = args
       }
       return fragment
   }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentPhotoPreviewDialogBinding.inflate(layoutInflater,container,false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (arguments?.get(IMG_PATH) != null){
            val imgPath:String = arguments?.get(IMG_PATH) as String
            binding.imgDialogPreviewPhoto.setImageBitmap(
                PictureUtils.getScaledBitmap(
                    imgPath,requireActivity()
                )
            )
        }
    }
}